<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="县区" prop="city" v-show="cityShow">
        <el-select
          v-model="queryParams.city"
          clearable
          placeholder="请选择"
          size="small"
          @change="selectCity"
        >
          <el-option
            v-for="item in this.cityData"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="乡镇" prop="town" v-show="townShow">
        <el-select
          v-model="queryParams.town"
          clearable
          placeholder="请选择"
          size="small"
          @change="selectTown"
        >
          <el-option
            v-for="item in this.townData"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="村" prop="village" v-show="villageShow">
        <el-select
          v-model="queryParams.village"
          clearable
          placeholder="请选择"
          size="small"
          @change="selectVillage"
        >
          <el-option
            v-for="item in this.villageData"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="承包方" prop="cbfmc">
        <el-input
          v-model="queryParams.cbfmc"
          placeholder="请输入承包方"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="附着物类型" prop="evaluationItemKeys">
        <el-select
          v-model="queryParams.ItemKeys"
          clearable
          placeholder="请选择"
          size="small"
          @change="selectFzw"
        >
          <el-option
            v-for="item in this.EvaluationList"
            :key="item.itemKey"
            :label="item.itemName"
            :value="item.itemKey">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="预算年份" prop="year">
        <el-date-picker
          v-model="queryParams.year"
          value-format="yyyy"
          type="year"
          placeholder="选择预算年份">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="面积" prop="cbfmc">
        <el-input
          v-model="queryParams.minArea"
          placeholder="最低值"
          clearable
          style="width: 93px"
          size="small"
          oninput="value=value.replace(/^\+|[^\d]/g,'')"
        />
        -
        <el-input
          v-model="queryParams.maxArea"
          placeholder="最高值"
          clearable
          style="width: 93px"
          size="small"
          oninput="value=value.replace(/^\+|[^\d]/g,'')"
        />
      </el-form-item>
      <el-form-item label="预算金额" prop="cbfmc">
        <el-input
          v-model="queryParams.minPrice"
          placeholder="最低值"
          clearable
          style="width: 93px"
          size="small"
          oninput="value=value.replace(/^\+|[^\d]/g,'')"
        />
        -
        <el-input
          v-model="queryParams.maxPrice"
          placeholder="最高值"
          clearable
          style="width: 93px"
          size="small"
          oninput="value=value.replace(/^\+|[^\d]/g,'')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-download"
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="infoList">
      <el-table-column min-width="220" label="行政区划" align="center" prop="county">
        <template slot-scope="scope">
          {{ scope.row.county }} - {{ scope.row.township }} - {{ scope.row.village }}
        </template>
      </el-table-column>
      <el-table-column label="承包方" min-width="120" align="center" prop="cbfmc"/>
      <el-table-column label="地块编码" min-width="180" align="center" prop="dkbm"/>
      <el-table-column label="预算年份" min-width="120" align="center" prop="year"/>
      <el-table-column label="评估项" min-width="120" align="center" prop="itemName"/>
      <el-table-column label="面积（亩）" min-width="90" align="center" prop="evaluationArea"/>
      <el-table-column label="预算金额（元）" min-width="120" align="center" prop="price">
        <template slot-scope="scope">
          {{
            scope.row.price == 0 ? "-" : (String(scope.row.price).replace(/(\d)(?=(?:\d{3})+$)/g, '$1,').split('.').length < 2 ? String(scope.row.price).replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + ".00" : String(scope.row.price).split('.')[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + "." + (String(scope.row.price).split('.')[1].length < 2 ? String(scope.row.price).split('.')[1] + '0' : String(scope.row.price).split('.')[1]))
          }}
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="查看明细" :visible.sync="open" width="800px" append-to-body>
      <div style="min-height: 484px">
        <el-table v-loading="loading" :data="detailsList">
          <el-table-column label="地块编码" width="200" align="center" prop="dkbm"/>
          <el-table-column label="地块名称" align="center" prop="dkmc"/>
          <el-table-column label="补偿项" align="center" prop="bcxmc"/>
          <el-table-column label="亩数" align="center" prop="area"/>
          <el-table-column label="评估价格" align="center" prop="money">
            <template slot-scope="scope">
              {{ scope.row.money == 0 ? "-" : String(scope.row.money).replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') }}
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listStatisDetail} from "@/api/zjgl/budgetFundsOut";
import {childlistUse} from "@/api/system/dept";
import {getInfo} from "@/api/login";
import {mxlistExport, mxlistReport} from "@/api/report/report";
import {listEvaluation} from "@/api/pggl/evaluation";

export default {
  name: "CapitalAccount",
  data() {
    return {
      city: '',
      town: '',
      village: '',
      cityShow: '',
      townShow: '',
      villageShow: '',
      cityData: [],
      townData: [],
      villageData: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 第三方管理表格数据
      infoList: [],
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        cbfbm: '',
        pageNum: 1,
        pageSize: 10,
        departmentCode: null,
        skrmc: null,
        year: null,
        evaluationItemKeys: null,
        ItemKeys: null,
        minArea: null,
        maxArea: null,
        minPrice: null,
        maxPrice: null,
        budgetIds: null,
      },
      EvaluationList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    //附着物信息
    listEvaluation({
      compensationType: 0, pageNum: 1,
      pageSize: 100,
    }).then(response => {
      this.EvaluationList = response.rows;
    });
    getInfo().then(response => {
      let deptCode = response.user.dept.deptCode;
      if (deptCode.length == 4) {//新区
        this.cityShow = true;
        this.townShow = true;
        this.villageShow = true;
        childlistUse(deptCode).then(response => {
          this.cityData = response.data;
        });
      } else if (deptCode.length == 6) {//县
        this.cityShow = false;
        this.townShow = true;
        this.villageShow = true;
        childlistUse(deptCode).then(response => {
          this.townData = response.data;
        });
      } else if (deptCode.length == 9) {//镇
        this.cityShow = false;
        this.townShow = false;
        this.villageShow = true;
        childlistUse(deptCode).then(response => {
          this.villageData = response.data;
        });
      } else {
        this.childlist(1306);
        this.cityShow = true;
        this.townShow = true;
        this.villageShow = true;
      }
    });
  },
  methods: {
    /** 导出按钮操作 */
    handleExport() {
      if (((this.queryParams.minArea == '' || this.queryParams.minArea == null) && (this.queryParams.maxArea != '' && this.queryParams.maxArea != null)) || ((this.queryParams.minArea != '' && this.queryParams.minArea != null) && (this.queryParams.maxArea == '' || this.queryParams.maxArea == null))) {
        this.$message.warning("请补全面积范围");
        return;
      }
      if (((this.queryParams.minPrice == '' || this.queryParams.minPrice == null) && (this.queryParams.maxPrice != '' && this.queryParams.maxPrice != null)) || ((this.queryParams.minPrice != '' && this.queryParams.minPrice != null) && (this.queryParams.maxPrice == '' || this.queryParams.maxPrice == null))) {
        this.$message.warning("请补全预算金额范围");
        return;
      }
      let _this = this;
      this.$confirm('是否确认导出?', "警告", {
        cancelButtonClass: 'btn-custom-cancel',
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        _this.newloading = _this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        return mxlistExport(_this.queryParams);
      }).then(response => {
        _this.newloading.close();
        this.download(response.msg);
      })
    },
    // 选择县
    selectCity(val) {
      this.city = val;
      this.queryParams.departmentCode = val;
      childlistUse(val).then(response => {
        this.townData = response.data;
      });
    },
    // 选择镇
    selectTown(val) {
      this.town = val;
      this.queryParams.departmentCode = val;
      childlistUse(val).then(response => {
        this.villageData = response.data;
      });
    },
    // 选择村
    selectVillage(val) {
      this.queryParams.departmentCode = val;
      this.village = val;
    },
    // 附着物
    selectFzw(val) {
      let evaluationItemKeys = [];
      evaluationItemKeys.push(val);
      this.queryParams.evaluationItemKeys = evaluationItemKeys.toString();
    },
    /** 查询第三方管理列表 */
    getList() {
      this.loading = true;
      mxlistReport(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        thirdId: null,
        apikey: null,
        name: null,
        address: null,
        contacts: null,
        phonenumber: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        maxArea: null,
        minArea: null,
        minPrice: null,
        maxPrice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (((this.queryParams.minArea == '' || this.queryParams.minArea == null) && (this.queryParams.maxArea != '' && this.queryParams.maxArea != null)) || ((this.queryParams.minArea != '' && this.queryParams.minArea != null) && (this.queryParams.maxArea == '' || this.queryParams.maxArea == null))) {
        this.$message.warning("请补全面积范围");
        return;
      }
      if (((this.queryParams.minPrice == '' || this.queryParams.minPrice == null) && (this.queryParams.maxPrice != '' && this.queryParams.maxPrice != null)) || ((this.queryParams.minPrice != '' && this.queryParams.minPrice != null) && (this.queryParams.maxPrice == '' || this.queryParams.maxPrice == null))) {
        this.$message.warning("请补全预算金额范围");
        return;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.cbfbm = '';
      this.queryParams.year = '';
      this.queryParams.maxArea = '';
      this.queryParams.minArea = '';
      this.queryParams.minPrice = '';
      this.queryParams.maxPrice = '';
      this.reset();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.thirdId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleDetails(row) {
      const thirdId = row.thirdId || this.ids
      listStatisDetail({
        cbfbm: row.cbfbm,
        cbfmc: row.cbfmc,
        batchNumber: row.batchNumber,
        fkzh: row.fkzh,
        skrkh: row.skrkh,
        skrmc: row.skrmc,
        ffsj: row.ffsj
      }).then(response => {
        this.detailsList = response.rows;
        this.open = true;
      });
    },
    /*  /!** 导出按钮操作 *!/
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有第三方管理数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return exportInfo(queryParams);
          }).then(response => {
            this.download(response.msg);
          })
      }*/
  }
};
</script>
<style>
.btn-custom-cancel {
  float: right;
  margin-left: 10px;
}
</style>
